﻿package com.fox2d 
{
	import flash.display.MovieClip;
	import flash.utils.Dictionary;
	
	/**
	 * ...
	 * @author Orlando Leite
	 */
	public class Char2D extends LiveObject2D
	{
		public static const STOPED		:uint = 0;
		public static const MOVE_NORTH	:uint = 1;
		public static const MOVE_SOUTH	:uint = 2;
		public static const MOVE_WEST	:uint = 3;
		public static const MOVE_EAST	:uint = 4;
		
		private var _speed:Number = 5;
		protected var direction:uint;
		private var walkDirection:uint = 0;
		private var _animation:Dictionary = new Dictionary();
		
		public function Char2D() 
		{
			super();
			
			referenceBitmap.x = - _reference.width / 2;
			referenceBitmap.y = - _reference.height / 2;
		}
		
		public function get speed():Number
		{
			return _speed;
		}
		
		public function set speed( value:Number ):void
		{
			_speed = value;
		}
		
		protected function addAnimation( type:uint, movie:MovieClip ):void
		{
			movie.stop();
			_animation[type] = movie;
		}
		
		protected function getAnimation( type:uint ):MovieClip
		{
			return _animation[type];
		}
		
		protected function moveNorth():void
		{
			direction = walkDirection = MOVE_NORTH;
			
			var temp:MovieClip = getAnimation( MOVE_NORTH );
			temp.gotoAndPlay( 2 );
			
			removeChild( container );
			
			if ( temp )
			{
				container = temp;
			}
			
			addChild( container );
		}
		
		protected function moveSouth():void
		{
			direction = walkDirection = MOVE_SOUTH;
			
			var temp:MovieClip = getAnimation( MOVE_SOUTH );
			temp.gotoAndPlay( 2 );
			
			removeChild( container );
			
			if ( temp )
			{
				container = temp;
			}
			
			addChild( container );
		}
		
		protected function moveWest():void
		{
			direction = walkDirection = MOVE_WEST;
			
			var temp:MovieClip = getAnimation( MOVE_WEST );
			temp.gotoAndPlay( 2 );
			
			removeChild( container );
			
			if ( temp )
			{
				container = temp;
			}
			
			addChild( container );
		}
		
		protected function moveEast():void
		{
			direction = walkDirection = MOVE_EAST;
			
			var temp:MovieClip = getAnimation( MOVE_EAST );
			temp.gotoAndPlay( 2 );
			
			removeChild( container );
			
			if ( temp )
			{
				container = temp;
			}
			
			addChild( container );
		}
		
		protected function stopMove():void
		{
			var temp:MovieClip = getAnimation( walkDirection );
			
			if ( !temp ) return;
			
			temp.gotoAndStop( 1 );
			
			removeChild( container );
			
			if ( temp )
			{
				container = temp;
			}
			
			addChild( container );
			
			walkDirection = STOPED;
		}
		
		protected function walk():void
		{
			switch( walkDirection )
			{
				case STOPED:
				break;
				
				case MOVE_NORTH:
				y -= speed;
				break;
				
				case MOVE_SOUTH:
				y += speed;
				break;
				
				case MOVE_EAST:
				x += speed;
				break;
				
				case MOVE_WEST:
				x -= speed;
				break;
			}
		}
		
		override public function update():void 
		{
			super.update();
			walk();
		}
		
	}
	
}